﻿'use strict';

/* ****************************************************************
WebServiceProxyDebug:
+ Events:
    + after
    + before
    + failed
    + success
**************************************************************** */

/*global module, exports, define, ActiveXObject*/
(function (global, factory) {
    if (typeof exports === 'object') {
        // Node
        module.exports = factory();
    }
    if (typeof define === 'function' && define.amd) {
        // AMD
        define(['webServiceProxy'], factory);
    }
    
    //// Browser globals
    //global.WebServiceProxyDebug = factory();

}(typeof window !== "undefined" ? window : this, function (WebServiceProxy) {
    var WebServiceProxyDebug = WebServiceProxy.extend({
        requestSuccess: function (method, data, response, options) {
            options || (options = {});

            this.trigger('before', this, method, data)
                .trigger(sprintf('before:%s', method), this, method, data);

            if (options.success)
                options.success(response);

            this.trigger('success', this, response, method)
                .trigger(sprintf('success:%s', method), this, response, method)
                .trigger('after', this, method)
                .trigger(sprintf('after:%s', method), this, method);

            return this;
        },
        requestFailed: function (method, data, error, options) {
            options || (options = {});

            this.trigger('before', this, method, data)
                .trigger(sprintf('before:%s', method), this, method, data);

            if (options.error)
                options.error(null, error, null);

            this.trigger('success', this, responseParsed, method)
                .trigger(sprintf('success:%s', method), this, response, method)
                .trigger('after', this, method)
                .trigger(sprintf('after:%s', method), this, method);

            return this;
        },
    });

    return WebServiceProxyDebug;
}));